package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.pc.model.TermpostModel;
import com.sbl.bajw.pc.service.DictModelService;
import com.sbl.bajw.pc.service.TermpostService;
import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/Termpost")
public class TermpostAction {
	
	@Resource
	private TermpostService service;
	
	@Resource
	private DictModelService dictService;
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("Termpost");
	}
	
	@RequestMapping(value = "/post")
	public @ResponseBody String post() throws MessageException{
		TermpostModel model = RequestUnit.getModel(TermpostModel.class);
		service.create(model);		
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("msg", "上传成功");
		result.put("success", "true");
		result.put("code", "1");
		String str = JsonUtil.ConvertObjectToJson(result);
		return str;
	}
	
	@RequestMapping(value = "/Edit")
	public @ResponseBody String Edit() throws MessageException{
		TermpostModel model = RequestUnit.getModel(TermpostModel.class);
		service.update(model);
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("msg", "上传成功");
		result.put("success", "true");
		result.put("code", "1");
		String str = JsonUtil.ConvertObjectToJson(result);
		return str;
	}
	
	@RequestMapping(value = "/getAttfile")
	public @ResponseBody String getAttfile(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String attfileid = RequestUnit.getString("attfileid");
		String result = service.getAttfile(attfileid);
		return result;
	}
	
	@RequestMapping(value = "/list")
	public @ResponseBody String list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userId = RequestUnit.getString("user_id");
		String startDate = RequestUnit.getString("start_date");
		String stopDate = RequestUnit.getString("stop_date");
		String cType = RequestUnit.getString("coll_type");
		String bType = RequestUnit.getString("building_type");
		String bCode = RequestUnit.getString("building_code");
		String addr = RequestUnit.getString("addr");
		String right = RequestUnit.getString("qualified");
		String json = "";
		String result = null;
		HashMap<String, Object> parm = new HashMap<String, Object>();
		log.info("条件搜索采集信息：userId="+userId+",startDate="+startDate+",stopDate="+stopDate+",cType="+cType+",bType="+bType+",bCode="+bCode+",right="+right);
		if(userId != null && !userId.equals("")){
			parm.put("userId", userId);
		}
		if(startDate != null && !"".equals(startDate)){
			parm.put("startDate", startDate);
		}
		if(stopDate != null && !"".equals(stopDate)){
			parm.put("stopDate", stopDate);
		}
		if(cType != null && !"".equals(cType)){
			parm.put("cType", cType);
		}
		if(bType != null && !"".equals(bType)){
			parm.put("bType", bType);
		}
		if(addr != null && !"".equals(addr)){
			parm.put("addr", "%"+addr+"%");
		}
		if(right != null && !"".equals(right)){
			parm.put("right", right);
		}
		result = service.list(parm);
		json += "{";
		json += "\"code\":\"1\",";
		json += "\"msg\":\"\",";
		json += "\"success\":\"true\",";
		json += "\"data\":" + result + "";
		json += "}";
		return json;
	}

	@RequestMapping(value = "/download")
	public void downloadList(HttpServletResponse response, HttpServletRequest request) throws Exception{
		
		String userId = request.getParameter("user_id");
		String startDate = RequestUnit.getString("start_date");
		String stopDate = RequestUnit.getString("stop_date");
		String cType = RequestUnit.getString("coll_type");
		String bType = RequestUnit.getString("building_type");
		String bCode = RequestUnit.getString("building_code");
		String addr = RequestUnit.getString("addr");
		String right = RequestUnit.getString("qualified");
		HashMap<String, String> parm = new HashMap<String, String>();
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
		log.info("条件搜索采集信息：userId="+userId+",startDate="+startDate+",stopDate="+stopDate+",cType="+cType+",bType="+bType+",bCode="+bCode);
		if(userId != null && !userId.equals("")){
			parm.put("userId", userId);
		}
		if(startDate != null && !"".equals(startDate)){
			parm.put("startDate", startDate);
		}
		if(stopDate != null && !"".equals(stopDate)){
			parm.put("stopDate", stopDate);
		}
		if(cType != null && !"".equals(cType)){
			parm.put("cType", cType);
		}
		if(bType != null && !"".equals(bType)){
			parm.put("bType", bType);
		}
		if(bCode != null && !"".equals(bCode)){
			parm.put("bCode", "%"+bCode+"%");
		}
		if(addr != null && !"".equals(addr)){
			parm.put("addr", "%"+addr+"%");
		}
		if(right != null && !"".equals(right)){
			parm.put("right", right);
		}
		list = service.getList(parm);
		LinkedHashMap<String, String> columns = new LinkedHashMap<String, String>();
		columns.put("ID", "编号");
		columns.put("TAKEDATE", "采集日期");
		columns.put("BUILDING_CODE", "楼栋编码");
		columns.put("BUILDING_TYPE", "楼栋类型");
		columns.put("ADDR", "地址");
		
		HSSFWorkbook wb = ExcelUtil.createExcel(list, columns);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=rental_housing.xls");  
		wb.write(outStream);
	}
	
	@RequestMapping(value = "/getTermpostModel")
	public @ResponseBody String getTermpostModel(HttpServletResponse response, HttpServletRequest request){
		String id = RequestUnit.getString("id");
		String userId = RequestUnit.getString("user_id");
		String json = "";
		log.info("采集信息详情：id="+id+",userId="+userId);
		HashMap<String, Object> result = service.getTermpostModel(id, userId);
		json += "{";
		json += "\"code\":\"1\",";
		json += "\"msg\":\"\",";
		json += "\"success\":\"true\",";
		json += "\"data\":" + JsonUtil.ConvertObjectToJson(result) + "";
		json += "}";
		return json;
	}
	
	@RequestMapping(value = "/TermpostListDetail")
	public @ResponseBody String TermpostListDetail(HttpServletResponse response, HttpServletRequest request){
		String code = RequestUnit.getString("building_code");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String json = null;
		if(code != null & !code.equals("")){
			parm.put("code", code);
			json = service.getTermpostListByCode(parm);
		}
		return json;
	}
	
	@RequestMapping(value = "/TermpostDetail")
	public ModelAndView TermpostDetail(HttpServletResponse response, HttpServletRequest request){
		String id = RequestUnit.getString("id");
		HashMap<String, Object> result = service.getTermpostModel(id, SessionUnit.getUserId());
		ModelAndView mv = new ModelAndView("/view/apartment/detail");
		request.setAttribute("model", result);
		return mv;
	}
	
	@RequestMapping(value = "/TermpostEdit")
	public ModelAndView TermpostEdit(HttpServletResponse response, HttpServletRequest request){
		String id = RequestUnit.getString("id");
		HashMap<String, Object> result = service.getTermpostModel(id, SessionUnit.getUserId());
		List<HashMap<String, Object>> infoDict = dictService.getInfoType();
		List<HashMap<String, Object>> bildDict = dictService.getBuildintTypeType();
		ModelAndView mv = new ModelAndView("/view/apartment/Edit");
		request.setAttribute("model", result);
		request.setAttribute("infoDict", infoDict);
		request.setAttribute("bildDict", bildDict);
		return mv;
	}
	
	@RequestMapping(value = "/listPage")
	public @ResponseBody String listPage(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String result = service.listPage();
		return result;
	}
	
	@RequestMapping(value = "/fuzzySearch")
	public @ResponseBody String fuzzySearch(HttpServletRequest request, HttpServletResponse response){
		String cons = RequestUnit.getString("cons");
		String userId = RequestUnit.getString("user_id");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String result = null;
		if(userId != null && !userId.equals("")){
			log.info("模糊搜索采集信息：cons="+cons+",userId="+userId);
			parm.put("userId", userId);
			if(cons != null && !"".equals(cons)){
				parm.put("cons", "%" + cons + "%");
			}
			result = service.fuzzySearch(parm);
		}
		String json = "";
		json += "{";
		json += "\"code\":\"1\",";
		json += "\"msg\":\"\",";
		json += "\"success\":\"true\",";
		json += "\"data\":" + result + "";
		json += "}";
		return json;
	}
	
	@RequestMapping(value = "/getZdryList")
	public @ResponseBody String getZdryList(HttpServletRequest request, HttpServletResponse response){
		String buildingCode = RequestUnit.getString("building_code");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String json = null;
		if(buildingCode != null && !buildingCode.equals("")){
			parm.put("buildingCode", buildingCode);
			json = service.getZdryList(parm);
		}
		return json;
	}
}